<?php

namespace App\Http\Controllers\Home;

use App\Model\Category;
use App\Model\FollowUser;
use App\Models\Archive\Archive;
use App\Models\Archive\ArchiveVisit;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;


class articleController extends Controller
{

    public function index()
    {
        //文章栏目
        $cate_id = Category::where('cate_name','好文')->value('cate_id');
        $cate_info =  Category::where('cate_id',$cate_id)->first();
        $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();

        foreach($category as $v){
            $v->archives = Archive::where('archive_type_id',1)->where('is_del',0)->where('category_id',$v->cate_id)->ofPattern('recommend')->ofPattern('review')->orderBy('updated_at','desc')->take(4)->get();
        }
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
            //置顶轮播
            $recommend = Archive::where('archive_type_id',1)->where('is_del',0)->ofPattern('top')->ofPattern('review')->orderBy('updated_at','desc')->take(4)->get();
            return view('mobile.article.article_list',compact('archives','category','cate_info'))->with('recommend',$recommend);

        }else{
            return view('home.article.article_list',compact('archives','category','cate_info'));
        }
    }

    public function category($category_id)
    {
        $cate_id = Category::where('cate_name','好文')->value('cate_id');
        $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();
        //顶部推荐
        $cate_info = Category::where('cate_id',$category_id)->first();
        $archives = Archive::where('archive_type_id',1)->where('category_id',$category_id)->ofPattern('review')->orderBy('updated_at','desc')->take(20)->get();
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
            return view('mobile.article.article_cate_list', compact('archives', 'category', 'cate_info'));
        }else{
            return view('home.article.article_cate_list',compact('archives','category','cate_info'));
        }
    }
    public function more()
    {
        if($input = Input::except('_token')){
            $archives = Archive::where('archive_type_id',1)->where('is_del',0)->where('category_id',$input['cate_id'])->ofPattern('review')->orderBy('updated_at','desc')->skip(20+$input['page']*8)->take(8)->get();

            foreach($archives as $v){
                $v->url = route('article.show', [$v->id]);
                $v->img = route('image', [trim($v->cover, '/'), '265x155']);
                $v->tags = $v->tags()->get()
                    ->map(function ($tag) {
                        $tag->url = $tag->url;
                        return $tag;
                    })
                ;
            }
            return response()->json($archives);
        }else{
            return 'error';
        }
    }
}
